// pages/bindPatient/index.js
const api = require("./../../config/api")
const request = require("./../../utils/request")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		patientInfo: '',
		appletUserDTOList: '',
		codetext: '发送验证码',
		second: 60,
		captcha:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

		const patien = JSON.parse(options.patientDate)
		const appletUserDTOList = patien.appletUserDTOList
		if(appletUserDTOList.length === 0){
			const item  = {
				phone: wx.getStorageSync('phone')
			}
			appletUserDTOList.push(item)
		}
		this.setData({
			patientInfo: patien,
			appletUserDTOList: patien.appletUserDTOList
		})
	},

	sendCode: async function() {
		const number = wx.getStorageSync('phone')
		const phone = this.data.appletUserDTOList[0].phone
		const data = {
			number: number,
			phone: phone
		}
		const res = await request.get(api.sedCode, data)
		if (res.httpStatus === 200) {
			console.log(res.data)
		}
		this.SetTime();
	},
	// 倒计时
	SetTime: function() {
		if (this.data.second == 0) {
			this.setData({
				disabled: false,
				codetext: "重新发送",
				second: 60
			})
		} else {
			this.setData({
				disabled: true,
				codetext: this.data.second + 's',
				second: this.data.second - 1
			})
			setTimeout(this.SetTime, 1000);
		}
	},
	code(e){
		this.setData({
			captcha:e.detail.value
		})
	},
	checkCode: async function() {
		const captcha = this.data.captcha
		const phone = this.data.appletUserDTOList[0].phone
		const data = {
			captcha: captcha,
			phone: phone
		}
		const res = await request.get(api.checkCode, data)
		if (res.httpStatus === 200) {
			console.log(res.data)
			this.bindPatient()
		}
	},
	bindPatient: async function() {
		const userId = wx.getStorageSync('userId')
		const patientId = this.data.patientInfo.id
		const data = {
			userId: userId,
			patientId: patientId
		}
		const res = await request.post(api.bindPatient, data)
		if (res.httpStatus === 200) {
			wx.showToast({
				title: '绑定成功',
				icon: "none",
			})
			setTimeout(function() {
				wx.navigateBack({
					delta: 2
				})
			}, 1000)
		}
	},
	unBind(val){
		console.log(val)
		const item = val.currentTarget.dataset.item
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
